package com.moly.grpc.server;

import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder;

import java.io.IOException;
import java.net.InetSocketAddress;

public class GrpcServer {


    private final Server server;

    public GrpcServer(int port ) {
//        this.server
        server = ServerBuilder.forPort(port).addService(new ServerRouteGuideImpl()).build();
        NettyServerBuilder
                .forAddress(new InetSocketAddress("localhost", 8999))
                .addService(new ServerRouteGuideImpl()).build();

    }

    public void start() throws IOException {

        server.start();
        Runtime.getRuntime().addShutdownHook(new Thread(){
            @Override
            public void run() {
                System.out.println("error");
            }
        });

    }
    public void  stop() {
        if(server !=null){
            server.shutdown();
        }
    }

    private void blockUntilShutdown() throws InterruptedException {
        if (server != null) {
            server.awaitTermination();
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        GrpcServer localhost = new GrpcServer( 8999);
        localhost.start();
        localhost.blockUntilShutdown();

    }


}
